package com.yj.account.service;

import com.yj.common.enums.OAUserLevel;
import com.yj.oa.entity.system.OADepartmentConfig;
import com.yj.oa.entity.system.OAUserConfig;
import com.yj.oa.entity.system.OAUserInfo;
import com.yj.oa.mapper.OAContractMapper;
import com.yj.oa.mapper.OADepartmentConfigMapper;
import com.yj.oa.mapper.OAUserConfigMapper;
import com.yj.oa.service.OAInfoService;
import com.yj.transport.mapper.TransportInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Copyright 2017 (C) Yunjian-VC
 * Created on : 2017/12/12
 * Author     : Unbelievable9
 **/
@Service
public class AccountService {

    @Autowired
    OAUserConfigMapper oaUserConfigMapper;

    @Autowired
    OADepartmentConfigMapper oaDepartmentConfigMapper;

    @Autowired
    OAContractMapper oaContractMapper;

    @Autowired
    TransportInfoMapper transportInfoMapper;

    @Autowired
    OAInfoService oaInfoService;

    public Map<String, Object> getContractInfoByUser(@NotNull OAUserInfo oaUserInfo) {
        Map<String, Object> contractInfoMap = new HashMap<>();

        OAUserConfig oaUserConfig = oaUserConfigMapper.getByUserID(oaUserInfo.getUser_id());

        OADepartmentConfig oaDepartmentConfig = oaDepartmentConfigMapper.getByDepartmentID(oaUserConfig.getDepartment_id());

        List<String> totalUserIDList = new ArrayList<>();

        if (oaDepartmentConfig.getDepartment_name().equals("总经办") ||
                oaDepartmentConfig.getDepartment_name().equals("董事办")) {
            Map<String, Object> subItemMap = oaInfoService.getSubItemMapByUser(oaUserInfo);

            List<Map<String, Object>> subDepartmentList = (List<Map<String, Object>>) subItemMap.get("list");

            List<Map<String, Object>> subDepartmentInfoList = new ArrayList<>();

            for (Map<String, Object> map : subDepartmentList) {
                Long department_id = (Long) map.get("id");
                String department_name = (String) map.get("name");

                Map<String, Object> departmentInfoMap = new HashMap<>();

                List<String> userIDList = new ArrayList<>();
                oaInfoService.findAllUserForDepartment(department_id, userIDList);

                BigDecimal amount = BigDecimal.ZERO;

                if (userIDList.size() > 0) {
                    amount = transportInfoMapper.sumGoodsWeightForUsers(userIDList);

                    if (amount == null) {
                        amount = BigDecimal.ZERO;
                    }
                }

                departmentInfoMap.put("id", department_id);
                departmentInfoMap.put("name", department_name);
                departmentInfoMap.put("amount", amount);

                subDepartmentInfoList.add(departmentInfoMap);
                totalUserIDList.addAll(userIDList);
            }

            contractInfoMap.put("sub", subDepartmentInfoList);
            contractInfoMap.put("level", OAUserLevel.AUTHORITY);
        } else {
            totalUserIDList.add(oaUserConfig.getUser_id());
            contractInfoMap.put("level", OAUserLevel.SALESMAN);
        }

        if (totalUserIDList.size() > 0) {
            contractInfoMap.put("delivering", oaContractMapper.countForDeliveringContract(totalUserIDList));
            contractInfoMap.put("finished", oaContractMapper.countForFinishedContract(totalUserIDList));

            BigDecimal amount = transportInfoMapper.sumGoodsWeightForUsers(totalUserIDList);

            if (amount == null) {
                amount = BigDecimal.ZERO;
            }

            contractInfoMap.put("amount", amount);
        }

        return contractInfoMap;
    }

}
